<?php

/**
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 */

if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}
$cron_lang = ROOT_PATH . 'languages/' .$GLOBALS['_CFG']['lang']. '/cron/order_info.php';
if (file_exists($cron_lang))
{
    global $_LANG;

    include_once($cron_lang);
}

/* 模块的基本信息 */
if (isset($set_modules) && $set_modules == TRUE)
{
    $i = isset($modules) ? count($modules) : 0;

    /* 代码 */
    $modules[$i]['code']    = basename(__FILE__, '.php');

    /* 描述对应的语言项 */
    $modules[$i]['desc']    = 'ws_order_desc';

    /* 作者 */
    $modules[$i]['author']  = 'xie';

    /* 网址 */
    $modules[$i]['website'] = '';

    /* 版本号 */
    $modules[$i]['version'] = '1.0.0';

    /* 配置信息 */
    $modules[$i]['config']  = '';

    return;
}

$num = 50;

// $where =  "WHERE oi.order_status=".OS_CONFIRMED;
// $where .= " AND oi.pay_status=".PS_PAYED;
// $where .= " AND oi.push_status=".PUS_NOTPUSH;
// $where .= " AND oi.shipping_status=".SS_UNSHIPPED;

// $sql = "SELECT COUNT(*) FROM ".
// 		$ecs->table('order_info')." AS oi ".
// 		$where;

// $total = $db->getOne($sql);

// while ($total>0)
// {
// 	push($limit);
// 	$total -= $limit;
// }

$where =  "WHERE oi.order_status=".OS_CONFIRMED;
$where .= " AND oi.pay_status=".PS_PAYED;
$where .= " AND oi.push_status=".PUS_PUSHED;
$where .= " AND oi.shipping_status IN(".SS_UNSHIPPED.",".SS_SHIPPED.",".SS_ROAD.",".SS_SHIPPED_PART.")";
$where .= " AND og.suppliers_id=1"; //威时管理的仓库

// $limit = " LIMIT ".$num;

$orderBy = " ORDER BY oi.add_time DESC ";
$groupBy = " GROUP BY oi.order_sn ";

$sql = "SELECT oi.* FROM ".
		$GLOBALS['ecs']->table('order_info')." AS oi ".
		" JOIN ".$GLOBALS['ecs']->table('order_goods')." AS og ON og.order_id=oi.order_id ".
		$where.$groupBy.$orderBy;

$orders = $GLOBALS['db']->getAll($sql);
// print_r($orders);

if (empty($orders)) return;
require_once(ROOT_PATH . '/' . ADMIN_PATH . '/includes/wtd.php');
require_once(ROOT_PATH . '/includes/lib_order.php');
require_once(ROOT_PATH . '/includes/lib_common.php');
require_once(ROOT_PATH . '/includes/lib_goods.php');
$temp = array();
foreach ($orders as $key=>$row)
{
	$temp[$key]['orderId'] = $row['pp_sn'];
// 	$temp[$key]['expressNo'] = null;
// 	$temp[$key]['state'] = null;
// 	print_r($temp);
	$result = query_express($temp[$key]);
	
// 	print_r($result);
	$fp2=@fopen(ROOT_PATH .'/log/ws_query.txt','a');
	fwrite($fp2, '****************************************'.PHP_EOL);
	fwrite($fp2,date('Y-m-d H:i:s').':'.json_encode($result).PHP_EOL);
	fwrite($fp2, '****************************************'.PHP_EOL);
	fclose($fp2);
	if ($result['code']!=1)
	{
		$error = empty($result['desc'])?'操作失败':$result['desc'];
		push_log($row['order_id'], $result['code'], $error, '查询威时订单');
		
	}
	else 
	{
		$where = " WHERE order_id='$row[order_id]' ";
		if (!empty($result['expressRoute']['expressNo']))
		{
			$invoiceStr = $order['invoice_no'];
			$expressNo = trim($result['expressRoute']['expressNo']);
			if (strpos($invoiceStr, $expressNo) === false)
			{
				$invoice_no = $invoiceStr==''?$expressNo:$invoiceStr.','.$expressNo;
				update_order($row['order_id'], array('invoice_no'=>$invoice_no));
			}
		}
		if (is_array($result['expressRoute']['expressRouteItemList']['expressRouteItems']))
		{
			foreach ($result['expressRoute']['expressRouteItemList']['expressRouteItems'] as $key=>$latest)
			{
				switch ($latest['state'])
				{
					case '20100':
// 						$status=4;
						break;
					case '20300':
// 						$status=5;
						break;
					case '20600':
// 						$status=6;
						break;
					case '20700':
// 						$status=7;
						
						break;
					case '50':
						$ship_status = $row['shipping_status'];
						$sql = '';
						// 					if ($ship_status==SS_SHIPPED_PART)
						// 					{
						// 						$sql = "UPDATE ".$GLOBALS['ecs']->table('order_info')." SET shipping_status=".SS_SHIPPED.$where;
						// 						sub_goods_frozen($row['order_id']);//处理库存
						// 					}
						// 					elseif ($ship_status==SS_UNSHIPPED)
						// 					{
						// 						$sql = "UPDATE ".$GLOBALS['ecs']->table('order_info')." SET shipping_status=".SS_SHIPPED_PART.$where;
						
						// 					}
						if ($ship_status==SS_UNSHIPPED)
						{
							$arr['shipping_status'] = SS_SHIPPED;
							$arr['shipping_time'] = gmtime();
							update_order($row['order_id'], $arr);
							if ($_CFG['use_storage'] == '1' && $_CFG['stock_dec_time'] == SDT_SHIP)
							{
								change_order_goods_storage($row['order_id'], true, SDT_SHIP, -1);
							}
							
							order_action($row['order_sn'], $row['order_status'], SS_RECEIVED, $row['pay_status'], '', '定时任务');
						}
						break;
					case '44':
						$ship_status = $row['shipping_status'];
						if (!in_array($ship_status,array(SS_ROAD,SS_SIGN,SS_RECEIVED))){
							update_order($row['order_id'], array('shipping_status'=>SS_ROAD));
							order_action($row['order_sn'], $row['order_status'], SS_ROAD, $row['pay_status'], '', '定时任务');
						}
						
						break;
					case '8000':
						$ship_status = $row['shipping_status'];
						if (!in_array($ship_status,array(SS_SIGN))){
							update_order($row['order_id'], array('shipping_status'=>SS_SIGN,'sign_time'=>gmtime()));
							order_action($row['order_sn'], $row['order_status'], SS_SIGN, $row['pay_status'], '', '定时任务');
						}
						break;
					case '80':
// 						$status=8;
						break;
					default:
						$status=0;
						break;
				}
				// 			print_r($latest);
				if ($status>0)
				{
					$sql = "UPDATE ".$GLOBALS['ecs']->table('order_info')." SET push_status=".$status.$where;
					$GLOBALS['db']->query($sql);
				}
			}
			

		}
		
	}
}










?>